package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotBattlePatrons;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotBuff;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotChange;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotMonsterData;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotUserData;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotChapterConfig;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotConfig;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotEquipConfig;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotMonsterConfig;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotSectionConfig;
import com.yanqu.road.entity.activity.seacraft.pirateriot.eliminate.SeacraftPirateRiotBlock;
import com.yanqu.road.entity.activity.seacraft.pirateriot.eliminate.SeacraftPirateRiotBoom;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.pb.activity.SeacraftPirateRiotProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftPirateRiotMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SeacraftPirateRiotPb {

    public static SeacraftPirateRiotProto.SeacraftPirateRiotSyncConfigRespMsg.Builder parseSeacraftPirateRiotSyncConfigRespMsg(SeacraftPirateRiotConfig config, String language) {
        SeacraftPirateRiotProto.SeacraftPirateRiotSyncConfigRespMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotSyncConfigRespMsg.newBuilder();
        for (SeacraftPirateRiotChapterConfig chapterConfig : config.getChapterConfigMap().values()) {
            builder.addChapterConfig(parseSeacraftPirateRiotChapterConfigMsg(chapterConfig, language));
        }
        for (Map<Integer, SeacraftPirateRiotSectionConfig> configMap : config.getSectionConfigMap().values()) {
            for (SeacraftPirateRiotSectionConfig sectionConfig : configMap.values()) {
                builder.addSectionConfig(parseSeacraftPirateRiotSectionConfigMsg(sectionConfig, language));
            }
        }
        for (SeacraftPirateRiotMonsterConfig monsterConfig : config.getMonsterConfigMap().values()) {
            builder.addMonsterConfig(parseSeacraftPirateRiotMonsterConfigMsg(monsterConfig, language));
        }
        for (SeacraftPirateRiotEquipConfig equipConfig : config.getEquipConfigMap().values()) {
            builder.addEquipConfig(parseSeacraftPirateRiotEquipConfigMsg(equipConfig, language));
        }
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotEquipConfigMsg.Builder parseSeacraftPirateRiotEquipConfigMsg(SeacraftPirateRiotEquipConfig config, String language) {
        SeacraftPirateRiotProto.SeacraftPirateRiotEquipConfigMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotEquipConfigMsg.newBuilder();
        builder.setLevel(config.getLevel());
        builder.setAttack(config.getAttack());
        builder.setHp(config.getHp());
        builder.setConsume(config.getConsume());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotChapterConfigMsg.Builder parseSeacraftPirateRiotChapterConfigMsg(SeacraftPirateRiotChapterConfig config, String language) {
        SeacraftPirateRiotProto.SeacraftPirateRiotChapterConfigMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotChapterConfigMsg.newBuilder();
        builder.setChapterId(config.getChapterId());
        builder.setSeaId(config.getSeaId());
        builder.setReward(config.getReward());
        builder.setFirstReward(config.getFirstReward());
        builder.setEnergy(config.getEnergy());
        builder.setHide(config.getHide());
        builder.setAdviceHp(config.getAdviceHp());
        builder.setAdviceAttack(config.getAdviceAttack());
        builder.setFailReward(config.getFailReward());
        builder.setRandomReward(config.getRandomReward());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotSectionConfigMsg.Builder parseSeacraftPirateRiotSectionConfigMsg(SeacraftPirateRiotSectionConfig config, String language) {
        SeacraftPirateRiotProto.SeacraftPirateRiotSectionConfigMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotSectionConfigMsg.newBuilder();
        builder.setChapterId(config.getChapterId());
        builder.setSectionId(config.getSectionId());
        builder.setMonsterId(config.getMonsterId());
        builder.setMonsterPos(config.getMonsterPos());
        builder.setMonsterBeHit(config.getMonsterBeHit());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotMonsterConfigMsg.Builder parseSeacraftPirateRiotMonsterConfigMsg(SeacraftPirateRiotMonsterConfig config, String language) {
        SeacraftPirateRiotProto.SeacraftPirateRiotMonsterConfigMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotMonsterConfigMsg.newBuilder();
        builder.setMonsterId(config.getMonsterId());
        builder.setQuality(config.getQuality());
        builder.setFirstRewards(config.getFirstReward());
        builder.setSeaId(config.getSeaId());
        builder.setSkillCd(config.getAttackCd());
        builder.setAnger(config.getAnger());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotUserDataMsg.Builder parseSeacraftPirateRiotUserDataMsg(GamePlayer player, SeacraftPirateRiotUserData userData) {
        SeacraftPirateRiotProto.SeacraftPirateRiotUserDataMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotUserDataMsg.newBuilder();
        builder.setEnergy(userData.getEnergy());
        builder.setRecoveryTime(userData.getLastRecTime());
        builder.addAllMonsterManual(new ArrayList<>(userData.getMonsterManual()));
        builder.addAllRewardMonster(new ArrayList<>(userData.getRewardMonster()));
        builder.setChapterId(userData.getChapterId());
        builder.setChapterStatus(userData.getChapterStatus());
        builder.setScore(userData.getScore());
        builder.setMaxChapterId(SeacraftPirateRiotMgr.getMaxChapterId(userData));
        builder.setEquipLevel(userData.getEquipLevel());
        builder.setAvgAttack(SeacraftPirateRiotMgr.getShowAvgAttack(userData));
        builder.setAvgHp(SeacraftPirateRiotMgr.getShowAvgHp(userData));
        builder.setHistoryChapterId((int) player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.SeacraftPirateRiotMaxChapterId));
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotChapterMsg.Builder parseSeacraftPirateRiotChapterMsg(SeacraftPirateRiotUserData userData) {
        SeacraftPirateRiotProto.SeacraftPirateRiotChapterMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotChapterMsg.newBuilder();
        builder.setChapterId(userData.getChapterId());
        builder.setSectionId(userData.getSectionId());
        builder.setChapterStatus(userData.getChapterStatus());
        builder.setData(parseSeacraftPirateGameDataMsg(userData));
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateGameDataMsg.Builder parseSeacraftPirateGameDataMsg(SeacraftPirateRiotUserData userData) {
        SeacraftPirateRiotProto.SeacraftPirateGameDataMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateGameDataMsg.newBuilder();
        int[][] blockList = userData.getBlockList();
        for (int y = 0; y < blockList.length; y++) {
            for (int x = 0; x < blockList[y].length; x++) {
                builder.addBlockList(parseSeacraftPirateBlockMsg(x, y, blockList[y][x]));
            }
        }
        for (SeacraftPirateRiotBattlePatrons patronsData : userData.getBattlePatronsMap().values()) {
            builder.addPatronsList(parseSeacraftPirateRiotPatronsMsg(patronsData));
        }
        for (SeacraftPirateRiotMonsterData monsterData : userData.getMonsterList()) {
            builder.addMonsterList(parseSeacraftPirateRiotMonsterMsg(monsterData));
        }
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotChangeMsg.Builder parseSeacraftPirateRiotChangeMsg(SeacraftPirateRiotChange change) {
        SeacraftPirateRiotProto.SeacraftPirateRiotChangeMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotChangeMsg.newBuilder();
        builder.setIndex(change.getIndex());
        if (change.getAddHp() != null) {
            builder.setAddHp(change.getAddHp());
        }
        if (change.getAddMp() != null) {
            builder.setAddMp(change.getAddMp());
        }
        if (change.getAddAttack() != null) {
            builder.setAddAttack(change.getAddAttack());
        }
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotBoomMsg.Builder parseSeacraftPirateRiotBoomMsg(SeacraftPirateRiotBoom boom) {
        SeacraftPirateRiotProto.SeacraftPirateRiotBoomMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotBoomMsg.newBuilder();
        builder.setX(boom.getX());
        builder.setY(boom.getY());
        builder.setType(boom.getType());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateBlockMsg.Builder parseSeacraftPirateBlockMsg(SeacraftPirateRiotBlock block) {
        SeacraftPirateRiotProto.SeacraftPirateBlockMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateBlockMsg.newBuilder();
        builder.setX(block.getX());
        builder.setY(block.getY());
        builder.setColor(block.getC());
        for (SeacraftPirateRiotChange change : block.getChangeList()) {
            builder.addChangeList(parseSeacraftPirateRiotChangeMsg(change));
        }
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateBlockMsg.Builder parseSeacraftPirateBlockMsg(int x, int y, int color) {
        SeacraftPirateRiotProto.SeacraftPirateBlockMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateBlockMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        builder.setColor(color);
        return builder;
    }

    public static List<SeacraftPirateRiotProto.SeacraftPirateBlockMsg> parseSeacraftPirateBlockMsgList(int[][] blockList) {
        List<SeacraftPirateRiotProto.SeacraftPirateBlockMsg> list = new ArrayList<>();
        for (int y = 0; y < blockList.length; y++) {
            for (int x = 0; x < blockList[y].length; x++) {
                list.add(parseSeacraftPirateBlockMsg(x, y, blockList[y][x]).build());
            }
        }
        return list;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotPatronsMsg.Builder parseSeacraftPirateRiotPatronsMsg(SeacraftPirateRiotBattlePatrons data) {
        SeacraftPirateRiotProto.SeacraftPirateRiotPatronsMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotPatronsMsg.newBuilder();
        builder.setPatronsId(data.getId());
        builder.setHp(data.getHp());
        builder.setHpMax(data.getHpMax());
        builder.setAttack(data.getAtk());
        builder.setMp(data.getMp());
        builder.setMpMax(data.getMpMax());
        for (SeacraftPirateRiotBuff buff : data.getBuffList()) {
            builder.addBuffList(parseSeacraftPirateRiotBuffMsg(buff));
        }
        return builder;
    }

    /**
     * 计算buff
     */
    public static SeacraftPirateRiotProto.SeacraftPirateRiotBuffMsg.Builder parseSeacraftPirateRiotBuffMsg(SeacraftPirateRiotBuff buff) {
        SeacraftPirateRiotProto.SeacraftPirateRiotBuffMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotBuffMsg.newBuilder();
        builder.setBuffType(buff.getType());
        builder.setRound(buff.getRound());
        builder.setParam(buff.getParam());
        builder.setPercent(buff.getPercent());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotMonsterMsg.Builder parseSeacraftPirateRiotMonsterMsg(SeacraftPirateRiotMonsterData data) {
        SeacraftPirateRiotProto.SeacraftPirateRiotMonsterMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotMonsterMsg.newBuilder();
        builder.setHp(data.getHp());
        builder.setMp(data.getMp());
        builder.setAttackCd(data.getAtkCd());
        for (SeacraftPirateRiotBuff buff : data.getBuffList()) {
            builder.addBuffList(parseSeacraftPirateRiotBuffMsg(buff));
        }
        builder.setHpMax(data.getHpMax());
        builder.setMpMax(data.getMpMax());
        builder.setAttack(data.getAtk());
        return builder;
    }

    public static SeacraftPirateRiotProto.SeacraftPirateRiotMonsterAttackMsg.Builder parseSeacraftPirateRiotMonsterAttackMsg(int monsterIndex, int type, List<SeacraftPirateRiotChange> list) {
        SeacraftPirateRiotProto.SeacraftPirateRiotMonsterAttackMsg.Builder builder = SeacraftPirateRiotProto.SeacraftPirateRiotMonsterAttackMsg.newBuilder();
        builder.setIndex(monsterIndex);
        builder.setType(type);
        for (SeacraftPirateRiotChange change : list) {
            builder.addChangeList(parseSeacraftPirateRiotChangeMsg(change));
        }
        return builder;
    }
}
